package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.repository.JpaRepository;
import cn.wolfcode.wolf2w.repository.CRUDJpaRepository;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class TJpaController {

    @Autowired
    private JpaRepository repository;

    @Autowired
    private CRUDJpaRepository crudJpaRepository;

    @Autowired
    private ElasticsearchTemplate esTemplate;

    @Autowired
    private  MongoTemplate template;

    @RequestMapping("jpa01")
    @ResponseBody
    public Object jpa01(String name){
        return repository.findByNameLike(name);
    }

    @RequestMapping("jpa02")
    @ResponseBody
    public Object jpa02(String name){
        List<Destination> destinations = repository.findByNameLike(name);
        Query query = new Query();
        query.addCriteria(Criteria.where("name").regex("沁水"));
        Update update = new Update();
        update.addToSet("english","qinshui");
        update.addToSet("info","大美沁水");
        template.updateMulti(query,update,Destination.class);
        return repository.findByNameLike(name);
    }

    @RequestMapping("jpa03")
    @ResponseBody
    public Object jpa03(String name){
        Destination destination = new Destination();
        destination.setId(null);
        destination.setName("沁水");
        destination.setParentName("晋城");
        destination.setParentId("5e2ab4f48847000059006eac");
        template.insert(destination);
        return repository.findByNameLike(name);
    }


    @RequestMapping("jpa04")
    @ResponseBody
    public Object jpa04(QueryObject qo){
        qo.setPageSize(2);
       Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize());
       Query query = new Query();
       query.addCriteria(Criteria.where("name").regex("水"));
       query.with(pageable);

       return template.find(query,Destination.class);

    }

    @RequestMapping("jpa05")
    @ResponseBody
    public Object jpa05(String name){

        return repository.findByNameLikeOrderByParentIdDesc(name);
    }

    @RequestMapping("jpa06")
    @ResponseBody
    public Object jpa06(){
        Map<String,List<StrategyCatalog>> destStrategyCatalog;
        Query query = new Query();
        System.out.println(template.find(query, StrategyCatalog.class));
//        List<String> destNames =
        return null;
    }
//    @RequestMapping("jpa07")
//    @ResponseBody
//    public Object jpa07(){
//        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//        builder.withQuery(QueryBuilders.termQuery("city","广州"));
//        esTemplate.query(builder,UserInfoEs.class)
////        page<>
//        return null;
//    }

}
